package com.comment.util.pay;

import cn.hutool.cache.impl.TimedCache;
import com.alibaba.fastjson2.JSON;
import com.comment.pojo.domain.logic.domain.Player;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * XOR加密解密工具类
 */
@Slf4j
public class XOREncryptDecrypt {
    private static final String KEY = "XOREncryptDecrypt";
    private static final TimedCache<String, String> cache = new TimedCache<>(60 * 1000);

    /**
     * 简单的XOR加密/解密函数
     *
     * @param data 输入字符串
     * @param key  加密/解密密钥
     * @return 加密/解密后的字符串
     */
    private static String xorEncryptDecrypt(String data, String key) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("Input string cannot be null or empty");
        }

        byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
        byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
        byte[] resultBytes = new byte[dataBytes.length];

        for (int i = 0; i < dataBytes.length; i++) {
            resultBytes[i] = (byte) (dataBytes[i] ^ keyBytes[i % keyBytes.length]);
        }

        return new String(resultBytes, StandardCharsets.UTF_8);
    }


    /**
     * 双端加密函数
     *
     * @param data 输入字符串
     * @param key  加密密钥
     * @return 加密后的字符串
     */
    private static String doubleEndedEncrypt(String data, String key) {
        // 对前半部分加密
        int mid = data.length() / 2;
        String firstHalfEncrypted = xorEncryptDecrypt(data.substring(0, mid), KEY);

        // 对后半部分加密
        String secondHalfEncrypted = xorEncryptDecrypt(data.substring(mid), key);

        // 将加密后的两部分拼接起来
        return firstHalfEncrypted + secondHalfEncrypted;
    }

    /**
     * 双端解密函数
     *
     * @param encryptedData 加密后的字符串
     * @param key           解密密钥
     * @return 解密后的字符串
     */
    private static String doubleEndedDecrypt(String encryptedData, String key) {
        int mid = encryptedData.length() / 2;
        String startLocal = encryptedData.substring(0, mid);
        String decryptedFirstHalf = xorEncryptDecrypt(startLocal, KEY);

        String endLocal = encryptedData.substring(mid);
        String decryptedSecondHalf = xorEncryptDecrypt(endLocal, key);

        return decryptedFirstHalf + decryptedSecondHalf;
    }


    public static String obtainKey(Long id) {
        String key = String.format("%s:$%d", KEY, id);
        // uuid
        String value = UUID.randomUUID().toString();
        // 生成无数值字符串
        value = value.replaceAll("[0-9]", "");
        value = value.replaceAll("-", "");
        value = "value";
        cache.put(key, value);
        return value;
    }

    //// 加密
    public static String xorEndedDecrypt(Long id, Object data) {
        String key = String.format("%s:$%d", KEY, id);
        String s1 = cache.get(key);
        log.info("使用支付密钥:{}", s1);
        cache.remove(key);
        String s2 = doubleEndedDecrypt(data.toString(), key);
        log.info("解密后:{}", s2);
        return s2;
    }

    public static void main(String[] args) {
        Player user = Player.createRobotPlayer(1L);
        String key2 = obtainKey(1L);
        log.info("获取的支付密钥:{}", key2);
        String json = JSON.toJSONString(user);
        log.info("加密前:{}", json);
        String doubleEndedEncrypt = doubleEndedEncrypt(json, key2);
        log.info("加密后:{}", doubleEndedEncrypt);
        String s = xorEndedDecrypt(1L, doubleEndedEncrypt);
    }
}
